#include "devcommon.h"

#include "invokers/invokerfactory.h"

#include <QFontMetrics>
#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include <QDir>
#include <QFileInfo>
#include <QDBusConnection>
#include <QDBusInterface>

#include <time.h>
#include <errno.h>

namespace Devcommon
{
bool registerTrustedExe(const QString &exePath)
{
    static DBusInvokerInterface* inter = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.systemconfig",
                                                                                     "/com/deepin/defender/systemconfig",
                                                                                     "com.deepin.defender.systemconfig",
                                                                                     ConnectType::SYSTEM, nullptr);
    QDBusMessage msg = DBUS_BLOCK_INVOKE(inter, "getTrustedExeList");
    GET_MESSAGE_VALUE(QStringList, list, msg);
    if (!(list.contains(exePath))) {
        DBUS_BLOCK_INVOKE(inter, "registerTrustedExe", exePath);
    }
    msg = DBUS_BLOCK_INVOKE(inter, "getTrustedExeList");
    GET_MESSAGE_VALUE(QStringList, relist, msg);
    if (relist.contains(exePath)) {
        return true;
    }
    return false;
}


bool loadTranslator(const QString &qm)
{
    QTranslator* translator = new QTranslator(qApp);
    if (translator->load(QLocale(), qm)) {
        qApp->installTranslator(translator);
    } else {
        return false;
    }
    return true;
}


bool loadTranslatorFromPath(const QString &path)
{
    QDir dir(path);
    QFileInfoList list = dir.entryInfoList(QStringList()<< "*.qm", QDir::Files|QDir::NoDotDot|QDir::NoDot);
    bool ret = true;
    for (const QFileInfo& fn : list) {
        ret &= loadTranslator(fn.filePath());
    }
    return ret;
}

void delay_ms(int milliseconds)
{
   struct timespec ts = {
       .tv_sec = milliseconds / 1000,
       .tv_nsec = (milliseconds % 1000) * 1000000
   };

   while ((nanosleep(&ts, &ts) == -1) && (errno == EINTR));
}

Qt::GlobalColor toColorType(const QColor &color)
{
    if (!color.isValid())
        return Qt::transparent;

    QColor rgb_color = color.toRgb();
    // 获取rgb颜色的亮度(转换为YUV格式)
    float luminance = 0.299 * rgb_color.redF() + 0.587 * rgb_color.greenF() + 0.114 * rgb_color.blueF();

    if (qRound(luminance * 255) > 191) {
        return Qt::white;
    }

    return Qt::black;
}

int adjustColorValue(int base, qint8 increment, int max)
{
    return increment > 0 ? (max - base) * increment / 100.0 + base
                         : base * (1 + increment / 100.0);
}

QColor adjustColor(const QColor &base, qint8 hueFloat, qint8 saturationFloat, qint8 lightnessFloat, qint8 redFloat, qint8 greenFloat, qint8 blueFloat, qint8 alphaFloat)
{
    // 按HSL格式调整
    int H, S, L, A;
    base.getHsl(&H, &S, &L, &A);

    H = H > 0 ? adjustColorValue(H, hueFloat, 359) : H;
    S = adjustColorValue(S, saturationFloat);
    L = adjustColorValue(L, lightnessFloat);
    A = adjustColorValue(A, alphaFloat);

    QColor new_color = QColor::fromHsl(H, S, L, A);

    // 按RGB格式调整
    int R, G, B;
    new_color.getRgb(&R, &G, &B);

    R = adjustColorValue(R, redFloat);
    G = adjustColorValue(G, greenFloat);
    B = adjustColorValue(B, blueFloat);

    new_color.setRgb(R, G, B, A);

    return new_color;
}

QRect calcStringSize(const QString &text, const QFont &font)
{
    QFontMetrics metrics(font);
    return metrics.boundingRect(text);
}

}
